package boardgamer.yavalath.client.mainmenu;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PointF;
import android.graphics.RectF;
import boardgamer.common.client.Drawable;
import boardgamer.common.client.layout.HasLayout;
import boardgamer.common.client.layout.LayoutGroup;
import boardgamer.yavalath.client.LetterHex;
import boardgamer.yavalath.client.RenderHelper;
import com.google.common.collect.ImmutableSet;

/**
 * @author garysoed
 */
public class Title implements HasLayout, Drawable {

  private final LayoutGroup letterGroup;
  private final ImmutableSet<LetterHex> horizontalLetters;
  private final ImmutableSet<LetterHex> verticalLetters;

  public Title() {
    this.letterGroup = new LayoutGroup();

    ImmutableSet.Builder<LetterHex> horizontalLettersBuilder
        = new ImmutableSet.Builder<LetterHex>();
    ImmutableSet.Builder<LetterHex> verticalLetterBuilder = new ImmutableSet.Builder<LetterHex>();

    PointF position = new PointF();
    horizontalLettersBuilder.add(new LetterHex('Y', Color.rgb(255, 0, 0), position,
        RenderHelper.instance().getHexLetterPaint(), false));

    position = offsetPosition(position, RenderHelper.instance().getHHexDownRightDisplacement());
    horizontalLettersBuilder.add(new LetterHex('A', Color.rgb(240, 0, 16), position,
        RenderHelper.instance().getHexLetterPaint(), false));

    position = offsetPosition(position, RenderHelper.instance().getHHexUpRightDisplacement());
    horizontalLettersBuilder.add(new LetterHex('V', Color.rgb(224, 0, 32), position,
        RenderHelper.instance().getHexLetterPaint(), false));

    position = offsetPosition(position, RenderHelper.instance().getHHexDownRightDisplacement());
    horizontalLettersBuilder.add(new LetterHex('A', Color.rgb(208, 0, 48), position,
        RenderHelper.instance().getHexLetterPaint(), false));

    position = offsetPosition(position, RenderHelper.instance().getHHexUpRightDisplacement());
    horizontalLettersBuilder.add(new LetterHex('L', Color.rgb(192, 0, 64), position,
        RenderHelper.instance().getHexLetterPaint(), false));

    position = offsetPosition(position, RenderHelper.instance().getHHexDownRightDisplacement());
    horizontalLettersBuilder.add(new LetterHex('A', Color.rgb(176, 0, 80), position,
        RenderHelper.instance().getHexLetterPaint(), false));

    position = offsetPosition(position, RenderHelper.instance().getHHexDownDisplacement());
    verticalLetterBuilder.add(new LetterHex('T', Color.rgb(160, 0, 96), position,
        RenderHelper.instance().getHexLetterPaint(), false));

    position = offsetPosition(position, RenderHelper.instance().getHHexDownDisplacement());
    verticalLetterBuilder.add(new LetterHex('H', Color.rgb(144, 0, 112), position,
        RenderHelper.instance().getHexLetterPaint(), false));

    this.horizontalLetters = horizontalLettersBuilder.build();
    for (LetterHex letter : horizontalLetters) {
      letterGroup.add(letter).atCurrentRect();
    }

    this.verticalLetters = verticalLetterBuilder.build();
    for (LetterHex letter : verticalLetters) {
      letterGroup.add(letter).atCurrentRect();
    }
  }

  public void draw(Canvas canvas) {
    for (LetterHex letter : horizontalLetters) {
      letter.draw(canvas);
    }
    for (LetterHex letter : verticalLetters) {
      letter.draw(canvas);
    }
  }

  private PointF offsetPosition(PointF original, PointF offset) {
    return new PointF(original.x + offset.x, original.y + offset.y);
  }

  public RectF getHorizontalLettersBoundingRect() {
    RectF horizontalBoundingRect = new RectF();
    for (LetterHex letter : horizontalLetters) {
      horizontalBoundingRect.union(letter.getBoundingRect());
    }
    return horizontalBoundingRect;
  }

  public RectF getVerticalLettersBoundingRect() {
    RectF verticalBoundingRect = new RectF();
    for (LetterHex letter : verticalLetters) {
      verticalBoundingRect.union(letter.getBoundingRect());
    }
    return verticalBoundingRect;
  }

  @Override
  public void setBoundingRect(RectF boundingRect) {
    this.letterGroup.setBoundingRect(boundingRect);
  }

  @Override
  public RectF getBoundingRect() {
    return this.letterGroup.getBoundingRect();
  }
}
